package com.xms.security.service.sitemap;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.datacore.service.FinderService;
import com.xms.security.entity.Privilege;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * PrivilegeFinderService
 * </p>
 *
 * @author migo
 * @since 2021/10/2
 */
@CacheConfig(cacheNames = "privilege", keyGenerator = "CacheKeyGenerator")
@Service
public class PrivilegeFinderService extends FinderService<Privilege> implements IPrivilegeFinderService {

    @Override
    @Cacheable(condition="#id != null")
    public Privilege getById(UUID id) {
        return super.getById(id);
    }

    @Override
    @Cacheable()
    public List<Privilege> getByIds(Collection<UUID> idList) {
        List<Privilege> result = new ArrayList<>();
        idList.forEach(x->{
            Privilege item = this.getById(x);
            if(item != null){
                result.add(item);
            }
        });
        return result;
    }

    @Override
    @Cacheable()
    public Privilege getOne(Wrapper<Privilege> queryWrapper) {
        return super.getOne(queryWrapper);
    }

    @Override
    @Cacheable()
    public int count() {
        return super.count();
    }

    @Override
    @Cacheable()
    public int count(Wrapper<Privilege> queryWrapper) {
        return super.count(queryWrapper);
    }

    @Override
    //@Cacheable()
    public List<Privilege> list(Wrapper<Privilege> queryWrapper) {
        return super.list(queryWrapper);
    }

    @Cacheable()
    @Override
    public Privilege getByUrl(String url) {
        LambdaQueryWrapper<Privilege> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Privilege::getUrl, url);
        return super.getOne(queryWrapper);
    }

    @Cacheable()
    @Override
    public Privilege getByName(String areaName, String className, String methodName) {
        LambdaQueryWrapper<Privilege> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Privilege::getSystemname, areaName)
                .eq(Privilege::getClassname, className).eq(Privilege::getMethodname, methodName);
        return super.getOne(queryWrapper);
    }

    @Cacheable()
    @Override
    public List<Privilege> getAuthorizations() {
        return super.list(new LambdaQueryWrapper<Privilege>().eq(Privilege::getAuthorizationenabled, true)
                .orderByAsc(Privilege::getLevel).orderByAsc(Privilege::getDisplayorder));
    }

    @Cacheable()
    @Override
    public List<Privilege> getVisibled() {
        return super.list(new LambdaQueryWrapper<Privilege>()
                .eq(Privilege::getIsvisibled, true)
                .orderByAsc(Privilege::getLevel)
                .orderByAsc(Privilege::getDisplayorder));
    }

    @Cacheable()
    @Override
    public List<Privilege> all() {
        return super.list(new LambdaQueryWrapper<Privilege>()
                .orderByAsc(Privilege::getLevel).orderByAsc(Privilege::getDisplayorder));
    }

}
